package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateWareHouseRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchWarehouseRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusWareHouseRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateWareHouseRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.WarehouseApiResponse;
import com.wzdigit.wms.basic.client.response.basic.WarehouseResponse;
import com.wzdigit.wms.basic.service.code.WhsPermService;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.domain.dto.FacDto;
import com.wzdigit.wms.wms.service.basic.WarehouseService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 仓库 前端控制器
 * @author yangdongjie
 * @WarehouseController.java
 * @2020年12月31日下午5:56:18
 * 
 */
@RestController
@Api(tags = "仓库")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;
    
    @Autowired
	private WhsPermService whsPremService;

    private static final String REQUEST_MODEL = "/warehouse";

    /**
     * 仓库（下拉框）列表（启用状态）
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "仓库（下拉框）列表（启用状态）",notes = "仓库（下拉框）列表（启用状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/list")
    public List<WarehouseApiResponse> list(
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return warehouseService.list();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 仓库（下拉框）列表（所有状态）
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "仓库（下拉框）列表（所有状态）",notes = "仓库（下拉框）列表（所有状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/listAll")
    public List<WarehouseApiResponse> listAll(
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return warehouseService.listAll();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
	/**
	 * 获取当前用户权限的仓库
	 * @param servletRequest
	 * @param servletResponse
	 * @return
	 */
	@ApiOperation(value = "获取当前用户权限的仓库",notes = "获取当前用户权限的仓库")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getWarehousePerm")
    public List<WarehouseApiResponse> getWarehousePerm(
    		@ApiParam(value = "仓库类型", required = false) 
    		@RequestParam(required = false) Integer whsType, 
    		HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return whsPremService.getWarehousePerm(whsType, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
    /**
     * 查询仓库列表
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @SuppressWarnings("rawtypes")
	@ApiOperation(value = "查询仓库列表",notes = "查询仓库列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchWarehouse")
    public PagerInfo<List<WarehouseResponse>> searchWarehouse(
    		SearchWarehouseRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return warehouseService.searchWarehouse(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 查询仓库
     * @param whsId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询仓库",notes = "查询仓库")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getWarehouse")
    public WarehouseResponse getWarehouse(
    		@ApiParam(value = "仓库ID", required = true) 
    		@RequestParam Integer whsId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return warehouseService.getWarehouse(whsId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 创建仓库
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建仓库",notes = "创建仓库")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createWareHouse")
    public WarehouseResponse createWareHouse(
    		@RequestBody @Valid CreateWareHouseRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return warehouseService.createWareHouse(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改仓库
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改仓库",notes = "修改仓库")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateWareHouse")
    public WarehouseResponse updateWareHouse(
    		@RequestBody @Valid UpdateWareHouseRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return warehouseService.updateWareHouse(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改仓库状态
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改仓库状态",notes = "修改仓库状态")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateStatus")
    public <T> Result<T> updateStatus(
    		@RequestBody @Valid UpdateStatusWareHouseRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return warehouseService.updateStatus(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    @ApiOperation(value = "获取仓库厂区下拉列表",notes = "获取仓库厂区下拉列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/updateStatus")
    public List<FacDto> getFacDto(HttpServletRequest servletRequest,
                                  HttpServletResponse servletResponse) {
        try {
            return warehouseService.getFacDto();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

}
